package com.umer.pinban.customer;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityRequest;
import com.common.okhttp.OkSimpleRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.adapter.customer.CustomerFragmentStatePagerAdapter;
import com.umer.pinban.base.BasicActivity;
import com.umer.pinban.bean.customer.CustomerBean;
import com.umer.pinban.customer.view.CustomerViewPager;
import com.umer.pinban.customer.view.RoundTextView;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.OnClick;
import static android.graphics.Color.parseColor;
import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

/**
 * 客户详情
 */
public class CustomerDetailsActivity extends BasicActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.linear_back_money)
    LinearLayout linearBackMoney;
    @BindView(R.id.tv_head)
    TextView tvHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_birthday_date)
    TextView tvBirthdayDate;
    @BindView(R.id.tv_birthday_lable)
    TextView tvBirthdayLable;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv1)
    RoundTextView tv1;
    @BindView(R.id.tv2)
    RoundTextView tv2;
    @BindView(R.id.tv3)
    RoundTextView tv3;
    @BindView(R.id.tv4)
    RoundTextView tv4;
    @BindView(R.id.tv5)
    RoundTextView tv5;
    @BindView(R.id.tv6)
    RoundTextView tv6;
    @BindView(R.id.tv7)
    RoundTextView tv7;
    @BindView(R.id.tv8)
    RoundTextView tv8;
    @BindView(R.id.linear_add)
    LinearLayout linearAdd;
    @BindView(R.id.linear_bg)
    LinearLayout linearBg;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.viewPager)
    CustomerViewPager viewPager;
    @BindView(R.id.tv9)
    RoundTextView tv9;
    private CustomerFragmentStatePagerAdapter customerFragmentStatePagerAdapter;
    private CustomerBean customerBean;
    private String menstrualStartDate = "";
    private String menstrualEndDate = "";
    private UserInfo userInfo;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_customer_details;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        customerBean = (CustomerBean) super.getIntent().getSerializableExtra("customer");
        if (customerBean != null) {
            setContents();
            initViews();
        }
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.linear_add, R.id.tv8, R.id.tv_one, R.id.tv_two, R.id.tv_three})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            //添加消费
            case R.id.linear_add:
                break;
            //经期设置
            case R.id.tv8:
               /* DialogTwo dialogTwo = new DialogTwo(this);
                dialogTwo.setEventBus(getEventBus());
                new XPopup.Builder(this)
                        .asCustom(dialogTwo)
                        .show();*/
                break;
            //消费
            case R.id.tv_one:
                setSelectStatus(0);
                viewPager.setCurrentItem(0);
                break;
            //提醒
            case R.id.tv_two:
                setSelectStatus(1);
                viewPager.setCurrentItem(1);
                break;
            //跟进
            case R.id.tv_three:
                setSelectStatus(2);
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        if(msgBean.getFlag() == EventConstants.select_time){
            List<String> time = (List<String>) msgBean.getObject();
            menstrualStartDate = time.get(0);
            menstrualEndDate = time.get(1);
            customerMenstrualSet();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what){
            case R.id.customerMenstrualSet:
                hideProgress();
                showToast(t.getDesc());
                selectCustomerInfo();
                break;
            case R.id.selectCustomerInfo:
                customerBean = (CustomerBean) t.getT();
                setContents();
                break;
            default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what){
            case R.id.customerMenstrualSet:
                hideProgress();
                showToast(t.getDesc());
                break;
            case R.id.selectCustomerInfo:
                break;
            default:break;
        }
    }

    private void selectCustomerInfo() {
        OkEntityRequest<CustomerBean> request = new OkEntityRequest<>(R.id.selectCustomerInfo,
                Constants.selectCustomerInfo, CustomerBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("customerId", customerBean.getCustomerId());
        requestOkhttpEntity(request);
        request.setHeader(true);
        requestOkhttpEntity(request);
    }

    private void customerMenstrualSet() {
        showProgress("正在请求...");
        OkSimpleRequest request = new OkSimpleRequest<>(R.id.customerMenstrualSet,
                Constants.customerMenstrualSet);
        request.addParams("customerId", customerBean.getCustomerId());
        request.addParams("shopId", userInfo.getShopId());
      /*  request.addParams("menstrualStartDate", menstrualStartDate +" 00:00:00");
        request.addParams("menstrualEndDate", menstrualEndDate +" 00:00:00");*/
        request.addParams("menstrualStartDate", dateToStrLong(strToDate(menstrualStartDate + " 00:00:00")));
        request.addParams("menstrualEndDate", dateToStrLong(strToDate(menstrualEndDate + " 00:00:00")));
        request.setHeader(true);
        requestOkhttpSimple(request);
    }

    public String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    private void setContents() {
        String name = customerBean.getName();
        if (name != null && !name.equals("") && !name.equals(" ") && !name.equals("null")) {
            tvHead.setText(name);
            String firstName = name.substring(0, 1);
            tvHead.setText(firstName);
            if (!isChinese(firstName)) {
                tvHead.setText(firstName.toUpperCase());
            }
            tvName.setText(name);
        } else {
            tvName.setText("");
        }
        String phone = customerBean.getPhone();
        if (phone != null && !phone.equals("") && !phone.equals(" ") && !phone.equals("null")) {
            //tvPhone.setText(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
            tvPhone.setText(phone);
        } else {
            tvPhone.setText("");
        }

        String birthday = customerBean.getBirthday();
        if (birthday != null && !birthday.equals("") && !birthday.equals(" ")
                && !birthday.equals("null")) {
            tvBirthdayDate.setVisibility(View.VISIBLE);
            tvBirthdayDate.setText(birthday);

        } else {
            tvBirthdayDate.setVisibility(View.GONE);
            tvBirthdayDate.setText(birthday);
        }
        String birthdayLabel = customerBean.getBirthdayLabel();
        if (birthdayLabel != null && !birthdayLabel.equals("") && !birthdayLabel.equals(" ")
                && !birthdayLabel.equals("null")) {
            tvBirthdayLable.setVisibility(View.VISIBLE);
            tvBirthdayLable.setText(birthdayLabel);

        } else {
            tvBirthdayLable.setVisibility(View.GONE);
            tvBirthdayLable.setText(birthday);
        }
        String address = customerBean.getAddress();
        if (address != null && !address.equals("") && !address.equals(" ")
                && !address.equals("null")) {
            tvAddress.setVisibility(View.VISIBLE);
            tvAddress.setText(address);

        } else {
            tvAddress.setVisibility(View.GONE);
            tvAddress.setText(birthday);
        }
        int makeStatus = customerBean.getMakeStatus();
        //明日没有预约
        if (makeStatus == 0) {
            tvHead.setBackgroundResource(R.drawable.shape9);
        }
        //明日有约
        else {
            tvHead.setBackgroundResource(R.drawable.shape10);
        }

        //1 所属优美师
        String personnelName = customerBean.getPersonnelName();
        if (personnelName != null && !personnelName.equals("") && !personnelName.equals(" ")
                && !personnelName.equals("null")) {
            tv1.setBackgroungColor1(parseColor("#FFC0CB"));
            tv1.setVisibility(View.VISIBLE);
            tv1.setText(personnelName + "(" + "美容师" + ")");
        } else {
            tv1.setText("");
            tv1.setVisibility(View.GONE);
        }

        //来源
        String customerSource = customerBean.getCustomerSource();
        if (customerSource != null && !customerSource.equals("") && !customerSource.equals(" ") && !customerSource.equals("null")) {
            if (customerSource.equals("转介绍")) {

                tv2.setBackgroungColor1(parseColor("#87CEFA"));
                String referrerCustomerName = customerBean.getReferrerCustomerName();
                if (referrerCustomerName != null && !referrerCustomerName.equals("") && !referrerCustomerName.equals(" ") && !referrerCustomerName.equals("null")) {
                    tv2.setVisibility(View.VISIBLE);
                    tv2.setText(referrerCustomerName + "(转)");
                } else {
                    tv2.setVisibility(View.GONE);
                    tv2.setText("");
                    tv2.setBackgroungColor1(parseColor("#FFFFFF"));
                }
            } else if (customerSource.equals("外拓")) {
                tv2.setVisibility(View.VISIBLE);
                tv2.setText(customerSource);
                tv2.setBackgroungColor1(parseColor("#4682B4"));
            } else if (customerSource.equals("美团")) {
                tv2.setVisibility(View.VISIBLE);
                tv2.setText(customerSource);
                tv1.setBackgroungColor1(parseColor("#FFD700"));
            } else if (customerSource.equals("体验客")) {
                tv2.setVisibility(View.VISIBLE);
                tv2.setText(customerSource);
                tv2.setBackgroungColor1(parseColor("#5F9EA0"));
            } else if (customerSource.equals("体验转")) {
                tv2.setVisibility(View.VISIBLE);
                tv2.setText(customerSource);
                tv2.setBackgroungColor1(parseColor("#5F9E64"));
            } else {
                tv2.setVisibility(View.VISIBLE);
                tv2.setText(customerSource);
                tv2.setBackgroungColor1(parseColor("#5F9E64"));
            }
        } else {
            tv2.setVisibility(View.GONE);
            tv2.setText("");
            tv2.setBackgroungColor1(parseColor("#FFFFFF"));
        }

        //3 多少天未进店
        String expenseSevenDayLabel = customerBean.getExpenseSevenDayLabel();
        if (expenseSevenDayLabel != null && !expenseSevenDayLabel.equals("") && !expenseSevenDayLabel.equals(" ")
                && !expenseSevenDayLabel.equals("null")) {
            tv3.setVisibility(View.VISIBLE);
            tv3.setText(expenseSevenDayLabel);
            String expenseSevenDayColor = customerBean.getExpenseSevenDayColor();
            if (expenseSevenDayColor != null && !expenseSevenDayColor.equals("") && !expenseSevenDayColor.equals(" ") && !expenseSevenDayColor.equals("null")) {
                tv3.setBackgroungColor1(parseColor(expenseSevenDayColor));
            } else {
                tv3.setBackgroungColor1(parseColor("#C2242B"));
            }
        } else {
            tv3.setVisibility(View.GONE);
            tv3.setText("");
            tv3.setBackgroungColor1(parseColor("#FFFFFF"));
        }

        //4 几天前消费
        String payThirtyDayLabel = customerBean.getPayThirtyDayLabel();
        if (payThirtyDayLabel != null && !payThirtyDayLabel.equals("") && !payThirtyDayLabel.equals(" ") && !payThirtyDayLabel.equals("null")) {
            String payThirtyDayColor = customerBean.getPayThirtyDayColor();
            tv4.setVisibility(View.VISIBLE);
            tv4.setText(payThirtyDayLabel);
            if (payThirtyDayColor != null && !payThirtyDayColor.equals("") && !payThirtyDayColor.equals(" ") && !payThirtyDayColor.equals("null")) {
                tv4.setBackgroungColor1(parseColor(payThirtyDayColor));
            } else {
                tv4.setBackgroungColor1(parseColor("#C2242B"));
            }
        } else {
            tv4.setVisibility(View.GONE);
            tv4.setText("");
            tv4.setBackgroungColor1(parseColor("#FFFFFF"));
        }

        //5体验转
        String experienceStatus = customerBean.getExperienceStatus();
        if (experienceStatus != null && !experienceStatus.equals("") && !experienceStatus.equals(" ") &&
                !experienceStatus.equals("null")) {
            if (experienceStatus.equals("1")) {
                tv5.setVisibility(View.VISIBLE);
                tv5.setText("体验");
                tv5.setBackgroungColor1(parseColor("#5F9EA0"));
            } else if (experienceStatus.equals("2")) {
                tv5.setVisibility(View.VISIBLE);
                tv5.setText("体验转");
                tv5.setBackgroungColor1(parseColor("#5F9E64"));
            } else {
                tv5.setVisibility(View.GONE);
                tv5.setText("");
            }
        } else {
            tv5.setVisibility(View.GONE);
            tv5.setText("");
        }

        //6类型
        String customerType = customerBean.getCustomerType();
        if (customerType != null && !customerType.equals("") && !customerType.equals(" ") &&
                !customerType.equals("null")) {
            tv6.setVisibility(View.VISIBLE);
            tv6.setText(customerType);
            tv6.setBackgroungColor1(parseColor("#4169e1"));
        } else {
            tv6.setVisibility(View.GONE);
        }

        //7等级
        String customerLevel = customerBean.getCustomerLevel();
        if (customerLevel != null && !customerLevel.equals("") && !customerLevel.equals(" ") &&
                !customerLevel.equals("null")) {
            tv7.setVisibility(View.VISIBLE);
            tv7.setText(customerLevel);
            tv7.setBackgroungColor1(parseColor("#6495ed"));
        } else {
            tv7.setVisibility(View.GONE);
        }

        //8经期
        String sex = customerBean.getSex();
        if (sex != null && !sex.equals("") && !sex.equals(" ") && !sex.equals("null")) {
            if (sex.equals("男")) {
                tv8.setVisibility(View.GONE);
                tv8.setText("男");
                tvSex.setText("男");
                tv8.setBackgroungColor1(parseColor("#888787"));
            } else {
                tv8.setVisibility(View.VISIBLE);
                tvSex.setText("女");
                if (customerBean.getMenstrualLabel() != null &&
                        !customerBean.getMenstrualLabel().equals("") && !customerBean.getMenstrualLabel().equals(" ")
                        && !customerBean.getMenstrualLabel().equals("null")) {
                    tv8.setText(customerBean.getMenstrualLabel());
                    String menstrualColor = customerBean.getMenstrualColor();
                    if (menstrualColor != null && !menstrualColor.equals("") && !menstrualColor.equals(" ") && !menstrualColor.equals("null")) {
                        tv8.setBackgroungColor1(parseColor(menstrualColor));
                    }
                } else {
                    tv8.setText("未设置经期");
                    tv8.setBackgroungColor1(parseColor("#888787"));
                }
            }
        } else {
            tvSex.setText("男");
            tv8.setVisibility(View.GONE);
            tv8.setText("男");
            tv8.setBackgroungColor1(parseColor("#888787"));
        }
        //9 顾客状态
        int customerStatus = customerBean.getCustomerStatus();
        if (customerStatus == -1 || customerStatus == 0) {
            tv9.setVisibility(View.VISIBLE);
            tv9.setBackgroungColor1(parseColor("#999999"));
        } else {
            tv9.setVisibility(View.GONE);
        }

    }

    private void initViews() {
        customerFragmentStatePagerAdapter = new CustomerFragmentStatePagerAdapter(getSupportFragmentManager(),
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        customerFragmentStatePagerAdapter.setFragments(getItems());
        viewPager.setAdapter(customerFragmentStatePagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(3);
    }

    private List<Fragment> getItems() {
        ArrayList<Fragment> items = new ArrayList<>();
        items.add(XiaoFeiFragment.newInstance(customerBean.getCustomerId()));
        items.add(TipsFragment.newInstance(customerBean.getCustomerId(), customerBean.getName()));
        items.add(GenJinFragment.newInstance(customerBean.getCustomerId()));
        return items;
    }

    @SuppressLint("ResourceAsColor")
    private void setSelectStatus(int index) {
        switch (index) {
            case 0:
                tvOne.setBackgroundResource(R.drawable.shape7);
                tvOne.setTextColor(parseColor("#ffffff"));
                tvTwo.setBackgroundResource(R.drawable.shape11);
                tvTwo.setTextColor(parseColor("#181818"));
                tvThree.setBackgroundResource(R.drawable.shape11);
                tvThree.setTextColor(parseColor("#181818"));
                break;
            case 1:
                tvOne.setBackgroundResource(R.drawable.shape11);
                tvOne.setTextColor(parseColor("#181818"));
                tvTwo.setBackgroundResource(R.drawable.shape7);
                tvTwo.setTextColor(parseColor("#ffffff"));
                tvThree.setBackgroundResource(R.drawable.shape11);
                tvThree.setTextColor(parseColor("#181818"));
                break;
            case 2:
                tvOne.setBackgroundResource(R.drawable.shape11);
                tvOne.setTextColor(parseColor("#181818"));
                tvTwo.setBackgroundResource(R.drawable.shape11);
                tvTwo.setTextColor(parseColor("#181818"));
                tvThree.setBackgroundResource(R.drawable.shape7);
                tvThree.setTextColor(parseColor("#ffffff"));
                break;
            default:
                break;
        }
    }

    // 判断一个字符是否是中文
    public boolean isChinese(String str) {
        for (int i = 0; i < str.length(); i++) {
            String bb = str.substring(i, i + 1);
            // 生成一个Pattern,同时编译一个正则表达式,其中的u4E00("一"的unicode编码)-\u9FA5("龥"的unicode编码)
            boolean cc = Pattern.matches("[\u4E00-\u9FA5]", bb);
            if (cc == false) {
                return cc;
            }
        }
        return true;
    }
}
